﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BigLotterySyndicate.Classes;
using SyndicateDAO.DatabaseObject;
using SyndicateBusiness.Classes;

namespace BigLotterySyndicate.Secured.Controls
{
    public partial class DrawListItem : BaseUserControl
    {
        public enum IsAlternate
        {
            True,
            False
        }
        private IsAlternate _alternateRow = IsAlternate.False;

        public IsAlternate AlternateRow
        {
            get { return _alternateRow; }
            set { _alternateRow = value; }
        }

        public enum PlayerView
        {
            True,
            False
        }
        private PlayerView _forPlayer = PlayerView.False;

        public PlayerView ForPlayer
        {
            get { return _forPlayer; }
            set { _forPlayer = value; }
        }

        public draw Draw
        {
            get;
            set;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Draw == default(draw))
            {
                this.Visible = false;
                return;
            }
        }

        #region print

        protected string PrintAlternate(string className = "alt-row")
        {
            return this.AlternateRow == IsAlternate.True ? className : " ";
        }

        public string PrintDrawType()
        {
            if (Draw != null)
                return String.Format("<img src=\"/Assets/Images/Logo{0}.png\" class=\"small-logo\" alt=\"{0}\" />",  this.Draw.type);
            else
                return "";
        }

        public string PrintDrawDate()
        {
            if (Draw != null)
                return String.Format("{0}", this.Draw.drawdate.ToShortDateString());
            else
                return "";
        }

        public string PrintPlayersCount()
        {
            if (Draw != null)
            {
                var players = this.BasePage.BusinessFacade.DrawBL.GetPlayers(Draw.id);

                return String.Format("{0}", players.Count());
            }
            else
                return "";
        }

        public string PrintLinesCount()
        {
            if (Draw != null)
            {
                var lines = this.BasePage.BusinessFacade.DrawBL.GetLines(Draw.id);

                return String.Format("{0}", lines.Count());
            }
            else
                return "";
        }

        public string PrintDrawStatus()
        {
            if (Draw != null)
            {
                return this.Draw.DrawStatus;
            }
            else
                return "";
        }

        public string PrintDrawResult()
        {
            if (Draw != null)
            {
                var numbers = this.BasePage.BusinessFacade.ResultBL.GetResultString(Draw.id);
                return !String.IsNullOrEmpty(numbers) ? numbers : "";
            }
            else
                return "";
        }

        public string PrintDrawSpent()
        {
            if (Draw != null)
            {
                double fundsUsed = 0;
                var linesCount = this.BasePage.BusinessFacade.LineBL.SelectAllByDrawId(Draw.id).Count();

                var pricePerLine = (int)Draw.pricePerLine;

                if (pricePerLine == 0)
                {
                    switch (Draw.type)
                    {
                        case draw.drawType.EUROMILLIONS:
                            fundsUsed += linesCount*2;
                            break;
                        default:
                            fundsUsed += linesCount;
                            break;
                    }
                }
                else
                {
                    fundsUsed += linesCount * (int)Draw.pricePerLine;
                }

                return String.Format("&pound;{0:0.00}", fundsUsed);
            }
            else
            {
                return "";
            }
        }

        public string PrintDrawWon()
        {
            if (Draw != null)
            {
                var winnings = Draw.winnings;
                if (winnings > 0)
                {
                    return String.Format("<span style=\"color:green;\">&pound;{0:0.00}</span>", winnings);
                }
                else
                {
                    return String.Format("&pound;{0:0.00}", winnings);
                }
            }
            else
            {
                return "";
            }
        }

        public string PrintDrawDetailsLink(string text = "view details")
        {
            if (Draw != null)
            {
                if(ForPlayer == PlayerView.False)
                return String.Format("<a href=\"/Secured/Draws/Default.aspx?Ns=2&dId={0}\">{1}</a>", Draw.id ,text);
                else
                    return String.Format("<a href=\"/MyAccount/Draws/Default.aspx?Ns=1&dId={0}\">{1}</a>", Draw.id, text);
            }
            else
                return "";
        }
        
        #endregion
    }
}